//
//  NSString+ZHWMD5.m
//  24-加密
//
//  Created by dllo on 16/8/16.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "NSString+ZHWMD5.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (ZHWMD5)
    
- (NSString *)stringWiht32BitMD5 {
    // 将字符串转换成C语言字符串
    const char *cString = [self UTF8String];
    // 将C的串转换 MD5
    CC_LONG length = (CC_LONG)strlen(cString);
    
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cString, length, bytes);
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [string appendFormat:@"%02x", bytes[i]];
    }
    return string;
}

- (NSString *)stringWith32BitMD5Lower {
    return [[self stringWiht32BitMD5] lowercaseString];
}

- (NSString *)stringWith32BitMD5Upper {
    return [[self stringWiht32BitMD5] uppercaseString];
}

- (NSString *)stringWiht16BitMD5 {
    NSString *str = [self stringWiht32BitMD5];
    NSString *md5String16Bit = [str substringToIndex:str.length - 8];
    md5String16Bit = [md5String16Bit substringFromIndex:7];
    return md5String16Bit;
}

- (NSString *)stringWith16BitMD5Lower {
    return [[self stringWiht16BitMD5] lowercaseString];
}

- (NSString *)stringWith16BitMD5Upper {
    return [[self stringWiht16BitMD5] uppercaseString];
}
@end
